package com.example.libraryms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.libraryms.exception.MyException;
import com.example.libraryms.mapper.BookTypeMapper;
import com.example.libraryms.model.dto.*;
import com.example.libraryms.model.entity.Book;
import com.example.libraryms.model.entity.BookType;
import com.example.libraryms.service.BookTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Cauli
 * @date 2023/7/31 13:56
 * @description 图书分类 服务类
 */
@Service
public class BookTypeServiceImpl extends ServiceImpl<BookTypeMapper, BookType> implements BookTypeService {
    @Override
    public Page<BookType> getBookTypePage(BookTypeQueryDTO bookTypeQueryDTO) {
        LambdaQueryWrapper<BookType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StrUtil.isNotEmpty(bookTypeQueryDTO.getName()), BookType::getName, bookTypeQueryDTO.getName())
                .eq(BookType::getDeleted, 0);
        return baseMapper.selectPage(bookTypeQueryDTO.toPage(), lambdaQueryWrapper);
    }

    @Override
    public BookTypeDTO getBookType(Integer bookTypeNo) {
        // 根据图书分类编号获取图书分类信息
        BookTypeDTO bookTypeDTO = null;
        BookType bookType = baseMapper.selectById(bookTypeNo);
        if (bookType == null) {
            throw new MyException("图书分类不存在");
        }
        bookTypeDTO = new BookTypeDTO();
        BeanUtils.copyProperties(bookType, bookTypeDTO);
        return bookTypeDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBookType(BookTypeAddDTO bookTypeAddDTO) {
        BookType bookType = new BookType();
        // 判断分类名称是否重复
        LambdaQueryWrapper<BookType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(bookTypeAddDTO.getName()), BookType::getName, bookTypeAddDTO.getName())
                .eq(BookType::getDeleted, 0);
        List<BookType> bookTypes = baseMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(bookTypes)) {
            throw new MyException("分类名称不能重复");
        }
        BeanUtil.copyProperties(bookTypeAddDTO, bookType);
        baseMapper.insert(bookType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBookType(BookTypeUpdateDTO bookTypeUpdateDTO) {
        // 根据图书分类编号获取图书分类信息
        BookType bookType = baseMapper.selectById(bookTypeUpdateDTO.getNo());
        if (bookType == null) {
            throw new MyException("图书分类不存在");
        }
        BeanUtil.copyProperties(bookTypeUpdateDTO, bookType);
        baseMapper.updateById(bookType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBookTypes(List<Integer> bookTypeNoList) {
        for (Integer no : bookTypeNoList) {
            // 根据图书分类编号获取图书分类信息
            BookType bookType = baseMapper.selectById(no);
            if (bookType == null) {
                throw new MyException("图书分类不存在");
            }
            // 查询该分类下是否有图书
            List<Book> books = baseMapper.selectBookByTypeNo(no);
            if (CollectionUtil.isNotEmpty(books)) {
                throw new MyException("不能删除有图书的分类");
            }
            baseMapper.deleteById(bookType);
        }
    }
}
